package com.fingard.dsp.bank.directbank.ccb02;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.ITransaction;
import com.fingard.dsp.bank.reqrespobj.enumeration.ENDirectRight;
import com.fingard.dsp.bank.reqrespobj.transaction.Req7515TodayTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.Req7516HisTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.RetAtsTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.detail.RetAtsTransDetail;
import com.fingard.text.StringHelper;
import com.fingard.util.DateHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.util.Calendar;
import java.util.Date;

public class CCB02Transaction extends CCB02Base implements ITransaction {

    //采用流处理方式
    //1、循环调用readRecord()读取一个请求明细，然后写入发银行的XML对象
    //2、发银行
    //3、循环调用XmlTextReader.read()读取一个银行返回节点，赋值返回ATS的明细对象，
    //   然后调用writeRetDetail()，写入返回ATS的明细
    public RetAtsTrans d_7516_HisTrans(Req7516HisTrans p_req7516) throws Exception {
        if (p_req7516.actBankType.equals("13")) {
            //判断结构账户标志是否现金池或者有直联现金池权限
            return d_6WY108_HisTrans(p_req7516);
        } else {
            return d_6WY101_HisTrans(p_req7516);
        }
    }

    //现金池明细查询
    public RetAtsTrans d_6WY108_HisTrans(Req7516HisTrans p_req7516) throws Exception {
        WriteBankLogLn("进入6WY108明细查询接口...");
        //历史明细查询返回ATS的对象
        RetAtsTrans retAts = new RetAtsTrans(p_req7516.reqHeader);
        String tmpRespCode = "";
        String tmpRespInfo = "";
        retAts.writeStart();//写返回ATS的统一头部信息
        try {
            Calendar tmpCalendarFrom = Calendar.getInstance();
            tmpCalendarFrom.setTime(p_req7516.getDateFrom());

            Calendar tmpCalendarTo = Calendar.getInstance();
            tmpCalendarTo.setTime(p_req7516.getDateTo());

            int tmpIntDateFrom = 0;
            int tmpIntDateTo = 0;
            //按自然月循环查询
            while (tmpIntDateFrom <= tmpIntDateTo && tmpRespCode.length() == 0) {
                Calendar tmpClndrStepTo = Calendar.getInstance();
                tmpClndrStepTo.setTime(tmpCalendarFrom.getTime());
                //判断是否跨月
                if (DateHelper.compareMonth(tmpCalendarFrom, tmpCalendarTo) == -1) {
                    //跨月则设置为月末
                    tmpClndrStepTo.set(Calendar.DAY_OF_MONTH, tmpClndrStepTo.getActualMaximum(Calendar.DAY_OF_MONTH));
                } else {
                    tmpClndrStepTo.setTime(tmpCalendarTo.getTime());
                }

                int tmpNowPage = 1;
                int tmpTotalPage = 0;
                String tmpPOSTSTR = "";
                while (true) {
                    CCB02Header bankHeader = new CCB02Header();
                    bankHeader.TX_CODE = "6WY108";
                    bankHeader.REQUEST_SN = getQryTransID();

                    XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                    //写入通用的头部节点
                    writeCommonHeader(xmlWriter, bankHeader);
                    xmlWriter.writeStartElement("TX_INFO");
                    if (!StringHelper.isNullOrEmpty(p_req7516.higherLevelAct)) {
                        xmlWriter.writeElementString("CZ_ACC_NO", p_req7516.higherLevelAct);//财资账号
                        xmlWriter.writeElementString("MEMB_ACC_NO", p_req7516.actNum);//成员账号
                    } else {
                        xmlWriter.writeElementString("CZ_ACC_NO", p_req7516.actNum);//财资账号
                        xmlWriter.writeElementClosed("MEMB_ACC_NO");//成员账号
                    }
                    xmlWriter.writeElementString("STARTDATE", Format.Date8Format.format(tmpCalendarFrom.getTime()));//开始时间 YYYYMMDD
                    xmlWriter.writeElementString("ENDDATE", Format.Date8Format.format(tmpClndrStepTo.getTime()));//结束时间 YYYYMMDD
                    xmlWriter.writeElementClosed("IS_SHOW_EXD");//是否展示集 默认否(0),1为是,0为否
                    xmlWriter.writeElementClosed("BARGAIN_FLAG");//交易方向 默认全部,1转入 0转出,不送为全部
                    xmlWriter.writeElementClosed("CHECK_ACC_NO");//对方账户
                    xmlWriter.writeElementClosed("CHECK_ACC_NAME");//对方账户名
                    xmlWriter.writeElementClosed("REMARK");//摘要
                    xmlWriter.writeElementClosed("LOW_AMT");//最小金额
                    xmlWriter.writeElementClosed("HIGH_AMT");//最大金额
                    xmlWriter.writeElementString("PAGE", String.valueOf(tmpNowPage));
                    xmlWriter.writeElementString("POSTSTR", tmpPOSTSTR);
                    xmlWriter.writeElementString("TOTAL_RECORD", "200");
                    xmlWriter.writeEndDocument();

                    String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), p_req7516.reqHeader.transType, p_req7516.reqHeader.transType);

                    XmlTextReader tmpBkReader;

                    if (tmpStrRet[0].length() == 0) {
                        StringBuilder tmpSbMsg = new StringBuilder();
                        tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                        if (tmpBkReader != null) {
                            String tmpRETURN_CODE = tmpBkReader.getFirstText("/TX/RETURN_CODE");
                            if (tmpRETURN_CODE.equals("8310ZX880002")) {
                                //记录为空
                                break;
                            } else if (tmpRETURN_CODE.equals("000000")) {
                                tmpTotalPage = Integer.parseInt(tmpBkReader.getFirstText("/TX/TX_INFO/TOTAL_PAGE"));
                                int tmpRetPage = Integer.parseInt(tmpBkReader.getFirstText("/TX/TX_INFO/PAGE"));
                                tmpPOSTSTR = tmpBkReader.getFirstText("/TX/TX_INFO/POSTSTR");
                                if (tmpRetPage != tmpNowPage && tmpTotalPage != 0) {
                                    //页次不相符
                                    tmpRespCode = "-1";
                                    tmpRespInfo = "请求页次" + tmpNowPage + "与响应页次" + tmpRetPage + "不相符";
                                    break;
                                } else {
                                    // #start "提取各返回节点内容"
                                    if (tmpTotalPage == 0)//记录为空
                                    {
                                        break;
                                    }
                                    while (tmpBkReader.read("/TX/TX_INFO/DETAILLIST/DETAILINFO")) {
                                        RetAtsTransDetail tmpRetDetail = new RetAtsTransDetail();
                                        tmpRetDetail.actNum = p_req7516.actNum;
                                        XElement tmpRetNode = tmpBkReader.readedXElement;
                                        tmpRetDetail.cur = convertCurToATS(tmpRetNode.getSingleText("CURR_COD"));//币种
                                        String tmpStrTRANDATE = tmpRetNode.getSingleText("TRANDATE");//交易日期
                                        if (tmpStrTRANDATE.length() > 8) {
                                            tmpStrTRANDATE = tmpStrTRANDATE.replace("/", "");
                                        }
                                        Date tmpTRANDATE = Format.Date8Format.parse(tmpStrTRANDATE);//"yyyyMMdd", null);
                                        tmpRetDetail.tranDate = Format.Date10Format.format(tmpTRANDATE);//tmpTRANDATE.ToString("yyyy-MM-dd");
                                        tmpRetDetail.valueDate = Format.Date10Format.format(tmpTRANDATE);//tmpTRANDATE.ToString("yyyy-MM-dd");

                                        String tmpStrTRANTIME = tmpRetNode.getSingleText("TRANTIME");//交易时间
                                        if (tmpStrTRANTIME.indexOf(":") > 0) {
                                            tmpStrTRANTIME = tmpStrTRANTIME.replace(":", "");
                                        }
                                        Date tmpTRANDATETIME;
                                        if (tmpStrTRANTIME.length() > 0) {
                                            tmpTRANDATETIME = Format.DateTime14Format.parse(tmpStrTRANDATE + tmpStrTRANTIME);//"yyyyMMddHHmmss", null);
                                        } else {
                                            tmpTRANDATETIME = Format.Date8Format.parse(tmpStrTRANDATE);//, "yyyyMMdd", null);
                                        }
                                        tmpRetDetail.tranTime = Format.DateTimeFormat.format(tmpTRANDATETIME);//tmpTRANDATETIME.ToString("yyyy-MM-dd HH:mm:ss");

                                        tmpRetDetail.billType = tmpRetNode.getSingleTextNotNull("CREDIT_TYPE").trim();
                                        tmpRetDetail.billNum = tmpRetNode.getSingleTextNotNull("CREDIT_NO").trim();

                                        String tmpBK_MESSAGE = tmpRetNode.getSingleTextNotNull("MESSAGE").trim();//摘要
                                        String tmpBK_DET = tmpRetNode.getSingleTextNotNull("DET").trim();//备注
                                        String messageToNote = getBankConfig().getValueAsString("messageToNote", "comment");
                                        if ("comment".equals(messageToNote)) {
                                            tmpRetDetail.comment = tmpBK_MESSAGE;
                                            tmpRetDetail.usage = tmpBK_DET;
                                        } else {
                                            tmpRetDetail.comment = tmpBK_DET;
                                            tmpRetDetail.usage = tmpBK_MESSAGE;
                                        }
//                                        tmpRetDetail.abstr = pickupAbstr(tmpBK_MESSAGE, tmpBK_DET);
                                        //ATSES-22232 通威目前在用的十个支付渠道,对账码识别截取优化 -----范天星/2020/01/19
                                        if (tmpBK_DET.contains("＋")) {
                                            tmpRetDetail.abstr = tmpBK_DET.substring(tmpBK_DET.lastIndexOf("＋") + 1, tmpBK_DET.length());
                                            tmpRetDetail.usage = tmpBK_DET.substring(0, tmpBK_DET.lastIndexOf("＋"));
                                        } else {
                                            tmpRetDetail.abstr = "";
                                            tmpRetDetail.usage = tmpRetDetail.usage.trim();
                                        }

                                        String tmpDR_AMT = tmpRetNode.getSingleText("DR_AMT");//借方发生额
                                        String tmpCR_AMT = tmpRetNode.getSingleText("CR_AMT");//贷方发生额
                                        if (!StringHelper.isNullOrEmpty(tmpDR_AMT) && Double.parseDouble(tmpDR_AMT) != 0) {
                                            //tmpRetDetail.cdFlag 1:借  2贷
                                            if (Double.parseDouble(tmpDR_AMT) < 0) {//借方发生额<0 判断为贷方
                                                tmpRetDetail.tranAmt = Double.toString(Math.abs(Double.parseDouble(tmpDR_AMT)));
                                                tmpRetDetail.cdFlag = "2";//贷
                                            } else {
                                                tmpRetDetail.tranAmt = tmpDR_AMT;
                                                tmpRetDetail.cdFlag = "1";//借
                                            }
                                        } else {
                                            if (Double.parseDouble(tmpCR_AMT) < 0) {//贷方发生额<0 判断为借方
                                                tmpRetDetail.tranAmt = Double.toString(Math.abs(Double.parseDouble(tmpCR_AMT)));
                                                tmpRetDetail.cdFlag = "1";//借
                                            } else {
                                                tmpRetDetail.tranAmt = tmpCR_AMT;
                                                tmpRetDetail.cdFlag = "2";//贷
                                            }
                                        }

                                        tmpRetDetail.accBal = tmpRetNode.getSingleText("BAL_AMT");

                                        tmpRetDetail.recAccNum = tmpRetNode.getSingleTextNotNull("ACC_NO2").trim();
                                        tmpRetDetail.recAccName = tmpRetNode.getSingleTextNotNull("ACC_NAME2").trim();
                                        tmpRetDetail.recAccBank = tmpRetNode.getSingleTextNotNull("OPPO_OPAC_BRCH").trim();
                                        //<TRAN_FLOW>交易流水号</TRAN_FLOW>
                                        tmpRetDetail.payCode = tmpRetNode.getSingleTextNotNull("TRAN_FLOW").trim();
                                        //<BFLOW>企业支付流水号</BFLOW>
                                        String receiptNo = tmpRetNode.getSingleTextNotNull("TRAN_FLOW").trim();
                                        WriteBankLogLn("处理前回单编号:" + receiptNo);
                                        receiptNo = antiTransferStr(receiptNo);
                                        WriteBankLogLn("处理后回单编号:" + receiptNo);
                                        tmpRetDetail.receiptNo = receiptNo;
                                        tmpRetDetail.askNumber = tmpRetNode.getSingleTextNotNull("TX_LOG_NO").trim();
                                        tmpRetDetail.reqBatchNO = tmpRetNode.getSingleTextNotNull("TX_LOG_NO").trim();
                                        tmpRetDetail.originator = tmpRetNode.getSingleText("TRAN_FLOW");

                                        tmpRetDetail.cashPoolBal = tmpRetNode.getSingleText("CASH_POOL_AMT");//现金池余额

                                        nodeAnalysic(tmpBkReader.readedXElement, tmpRetDetail, getCharset());

                                        retAts.writeRetDetail(tmpRetDetail);
                                    }
                                    // #end
                                }
                            } else {
                                // #start "查询失败"
                                tmpRespCode = "-1";
                                tmpRespInfo = tmpBkReader.getFirstText("/TX/RETURN_MSG");
                                break;
                                // #end
                            }
                        } else {
                            tmpRespCode = "-1";
                            tmpRespInfo = tmpSbMsg.toString();
                            break;
                        }
                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = tmpStrRet[1];
                        break;
                    }
                    if (tmpNowPage >= tmpTotalPage) {
                        break;
                    }
                    tmpNowPage++;
                }

                tmpCalendarFrom = tmpClndrStepTo;
                tmpCalendarFrom.add(Calendar.DAY_OF_MONTH, 1);
                tmpIntDateFrom = Integer.parseInt(Format.Date8Format.format(tmpCalendarFrom.getTime()));
                tmpIntDateTo = Integer.parseInt(Format.Date8Format.format(tmpCalendarTo.getTime()));
            }
        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        if (tmpRespCode.equals("-1")) {
            retAts = new RetAtsTrans(p_req7516.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("交易为空");
        }
        retAts.writeEndWithoutInsertDB();
        return retAts;
    }

    //普通明细查询
    public RetAtsTrans d_6WY101_HisTrans(Req7516HisTrans p_req7516) throws Exception {
        WriteBankLogLn("进入6WY101明细查询接口...");
        //历史明细查询返回ATS的对象
        RetAtsTrans retAts = new RetAtsTrans(p_req7516.reqHeader);
        String tmpRespCode = "";
        String tmpRespInfo = "";
        retAts.writeStart();//写返回ATS的统一头部信息
        try {

            //Date tmpDateFrom = p_req7516.getDateFrom();
            //Date tmpDateTo = p_req7516.getDateTo();

            //tmpClndrStepTo.get(Calendar.MONTH) MONTH是从0开始编的，意味着0是1月，11是12月
            Calendar tmpCalendarFrom = Calendar.getInstance();
            tmpCalendarFrom.setTime(p_req7516.getDateFrom());

            Calendar tmpCalendarTo = Calendar.getInstance();
            tmpCalendarTo.setTime(p_req7516.getDateTo());

            int tmpIntDateFrom = 0;
            int tmpIntDateTo = 0;
            //按自然月循环查询
            while (tmpIntDateFrom <= tmpIntDateTo && tmpRespCode.length() == 0) {
                Calendar tmpClndrStepTo = Calendar.getInstance();
                tmpClndrStepTo.setTime(tmpCalendarFrom.getTime());
                //判断是否跨月
                if (DateHelper.compareMonth(tmpCalendarFrom, tmpCalendarTo) == -1) {
                    //跨月则设置为月末
                    tmpClndrStepTo.set(Calendar.DAY_OF_MONTH, tmpClndrStepTo.getActualMaximum(Calendar.DAY_OF_MONTH));
                } else {
                    tmpClndrStepTo.setTime(tmpCalendarTo.getTime());
                }

                int tmpNowPage = 1;
                int tmpTotalPage = 0;
                String tmpPOSTSTR = "";
                while (true) {
                    CCB02Header bankHeader = new CCB02Header();
                    bankHeader.TX_CODE = "6WY101";
                    bankHeader.REQUEST_SN = getQryTransID();

                    XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                    //写入通用的头部节点
                    writeCommonHeader(xmlWriter, bankHeader);
                    xmlWriter.writeStartElement("TX_INFO");
                    xmlWriter.writeElementString("ACCNO1", p_req7516.actNum);
                    xmlWriter.writeElementString("STARTDATE", Format.Date8Format.format(tmpCalendarFrom.getTime()));
                    xmlWriter.writeElementString("ENDDATE", Format.Date8Format.format(tmpClndrStepTo.getTime()));
                    xmlWriter.writeElementClosed("BARGAIN_FLAG");
                    xmlWriter.writeElementClosed("CHECK_ACC_NO");
                    xmlWriter.writeElementClosed("CHECK_ACC_NAME");
                    xmlWriter.writeElementClosed("REMARK");
                    xmlWriter.writeElementClosed("LOW_AMT");
                    xmlWriter.writeElementClosed("HIGH_AMT");
                    xmlWriter.writeElementString("PAGE", String.valueOf(tmpNowPage));
                    xmlWriter.writeElementString("POSTSTR", tmpPOSTSTR);
                    xmlWriter.writeElementString("TOTAL_RECORD", "200");
                    xmlWriter.writeElementClosed("DET_NO");
                    xmlWriter.writeEndDocument();

                    String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
//                    String[] tmpStrRet = new String[]{"", "<?xml version=\"1.0\" encoding=\"GB18030\"?>\n" +
//                            "<TX>\n" +
//                            "    <REQUEST_SN>200116090722922</REQUEST_SN>\n" +
//                            "    <CUST_ID>R510000000044041#0H</CUST_ID>\n" +
//                            "    <TX_CODE>6WY101</TX_CODE>\n" +
//                            "    <RETURN_CODE>000000</RETURN_CODE>\n" +
//                            "    <RETURN_MSG></RETURN_MSG>\n" +
//                            "    <LANGUAGES>CN</LANGUAGES>\n" +
//                            "    <TX_INFO>\n" +
//                            "        <ACCNO1>54001023636059669999</ACCNO1>\n" +
//                            "        <CURR_COD>01</CURR_COD>\n" +
//                            "        <ACC_NAME>通威实业（西藏）有限公司</ACC_NAME>\n" +
//                            "        <ACC_ORGAN>540000000</ACC_ORGAN>\n" +
//                            "        <ACC_STATE>正常</ACC_STATE>\n" +
//                            "        <INTR>0.00</INTR>\n" +
//                            "        <TOTAL_PAGE>1</TOTAL_PAGE>\n" +
//                            "        <PAGE>1</PAGE>\n" +
//                            "        <POSTSTR>+2+1+2+</POSTSTR>\n" +
//                            "        <FLAG>0</FLAG>\n" +
//                            "        <FILE_LOCSTR></FILE_LOCSTR>\n" +
//                            "        <DETAILLIST>\n" +
//                            "            <DETAILINFO>\n" +
//                            "                <TRANDATE>2020/01/16</TRANDATE>\n" +
//                            "                <TRANTIME>09:06:52</TRANTIME>\n" +
//                            "                <CRE_TYP>电子转账凭证</CRE_TYP>\n" +
//                            "                <CRE_NO>102783756002</CRE_NO>\n" +
//                            "                <MESSAGE>网络转账</MESSAGE>\n" +
//                            "                <AMT>0.01</AMT>\n" +
//                            "                <AMT1>1944.80</AMT1>\n" +
//                            "                <FLAG1>0</FLAG1>\n" +
//                            "                <ACCNO2>22900901040001067</ACCNO2>\n" +
//                            "                <ACC_NAME1>通威实业（西藏）有限公司</ACC_NAME1>\n" +
//                            "                <FLAG2>0</FLAG2>\n" +
//                            "                <TRAN_FLOW>5400236360NDP7L74Z2</TRAN_FLOW>\n" +
//                            "                <BFLOW>0000113075</BFLOW>\n" +
//                            "                <DET_NO>1436</DET_NO>\n" +
//                            "                <DET>资金调拨＋CC000015KW</DET>\n" +
//                            "                <REAL_TRANDATE>2020/01/16</REAL_TRANDATE>\n" +
//                            "                <RLTV_ACCNO></RLTV_ACCNO>\n" +
//                            "                <CADBank_Nm>中国农业银行股份有限公司成都航天城支行</CADBank_Nm>\n" +
//                            "                <Ovrlsttn_Trck_No>1080020321579136811263052</Ovrlsttn_Trck_No>\n" +
//                            "                <ExoStm_Py_Rmrk>0000113075</ExoStm_Py_Rmrk>\n" +
//                            "            </DETAILINFO>\n" +
//                            "            <DETAILINFO>\n" +
//                            "                <TRANDATE>2020/01/16</TRANDATE>\n" +
//                            "                <TRANTIME>09:06:54</TRANTIME>\n" +
//                            "                <CRE_TYP>电子转账凭证</CRE_TYP>\n" +
//                            "                <CRE_NO>102783756059</CRE_NO>\n" +
//                            "                <MESSAGE>网络转账</MESSAGE>\n" +
//                            "                <AMT>0.01</AMT>\n" +
//                            "                <AMT1>1944.79</AMT1>\n" +
//                            "                <FLAG1>0</FLAG1>\n" +
//                            "                <ACCNO2>6230520460000304075</ACCNO2>\n" +
//                            "                <ACC_NAME1>邓婷</ACC_NAME1>\n" +
//                            "                <FLAG2>0</FLAG2>\n" +
//                            "                <TRAN_FLOW>5400236360NKP3NT8MH</TRAN_FLOW>\n" +
//                            "                <BFLOW>0000113076</BFLOW>\n" +
//                            "                <DET_NO>1437</DET_NO>\n" +
//                            "                <DET>测试＋CC000015KX</DET>\n" +
//                            "                <REAL_TRANDATE>2020/01/16</REAL_TRANDATE>\n" +
//                            "                <RLTV_ACCNO></RLTV_ACCNO>\n" +
//                            "                <CADBank_Nm>中国农业银行股份有限公司成都东大街支行</CADBank_Nm>\n" +
//                            "                <Ovrlsttn_Trck_No>1080020401579136814261092</Ovrlsttn_Trck_No>\n" +
//                            "                <ExoStm_Py_Rmrk>0000113076</ExoStm_Py_Rmrk>\n" +
//                            "            </DETAILINFO>\n" +
//                            "        </DETAILLIST>\n" +
//                            "    </TX_INFO>\n" +
//                            "</TX>"};

                    XmlTextReader tmpBkReader;

                    if (tmpStrRet[0].length() == 0) {
                        StringBuilder tmpSbMsg = new StringBuilder();
                        tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                        if (tmpBkReader != null) {
                            String tmpRETURN_CODE = tmpBkReader.getFirstText("/TX/RETURN_CODE");
                            if (tmpRETURN_CODE.equals("8310ZX880002"))//记录为空
                            {
                                break;
                            } else if (tmpRETURN_CODE.equals("000000")) {
                                tmpTotalPage = Integer.parseInt(tmpBkReader.getFirstText("/TX/TX_INFO/TOTAL_PAGE"));
                                int tmpRetPage = Integer.parseInt(tmpBkReader.getFirstText("/TX/TX_INFO/PAGE"));
                                tmpPOSTSTR = tmpBkReader.getFirstText("/TX/TX_INFO/POSTSTR");
                                if (tmpRetPage != tmpNowPage && tmpTotalPage != 0) {
                                    // #start 页次不相符
                                    tmpRespCode = "-1";
                                    tmpRespInfo = "请求页次" + tmpNowPage + "与响应页次" + tmpRetPage + "不相符";
                                    break;
                                    // #end
                                } else {
                                    // #start "提取各返回节点内容"
                                    if (tmpTotalPage == 0)//记录为空
                                    {
                                        break;
                                    }
                                    while (tmpBkReader.read("/TX/TX_INFO/DETAILLIST/DETAILINFO")) {
                                        RetAtsTransDetail tmpRetDetail = new RetAtsTransDetail();
                                        XElement tmpRetNode = tmpBkReader.readedXElement;
                                        String tmpStrTRANDATE = tmpRetNode.getSingleText("TRANDATE");

                                        String tmp_REAL_TRANDATE = tmpRetNode.getSingleText("REAL_TRANDATE");
                                        if (StringHelper.isNullOrEmpty(tmp_REAL_TRANDATE)) {
                                            tmp_REAL_TRANDATE = tmpStrTRANDATE;
                                        }

                                        String tmpStrTRANTIME = tmpRetNode.getSingleText("TRANTIME");
                                        tmpRetDetail.postTimestamp = tmp_REAL_TRANDATE + " " + tmpStrTRANTIME;
                                        if (tmpStrTRANDATE.length() > 8) {
                                            tmpStrTRANDATE = tmpStrTRANDATE.replace("/", "");
                                        }
                                        if (tmp_REAL_TRANDATE.length() > 8) {
                                            tmp_REAL_TRANDATE = tmp_REAL_TRANDATE.replace("/", "");
                                        }
                                        if (tmpStrTRANTIME.indexOf(":") > 0) {
                                            tmpStrTRANTIME = tmpStrTRANTIME.replace(":", "");
                                        }
                                        Date tmpTRANDATE = Format.Date8Format.parse(tmpStrTRANDATE);//"yyyyMMdd", null);
                                        Date tmpTRANDATETIME;
                                        if (tmpStrTRANTIME.length() > 0) {
                                            tmpTRANDATETIME = Format.DateTime14Format.parse(tmp_REAL_TRANDATE + tmpStrTRANTIME);//"yyyyMMddHHmmss", null);
                                        } else {
                                            tmpTRANDATETIME = Format.Date8Format.parse(tmp_REAL_TRANDATE);//, "yyyyMMdd", null);
                                        }
                                        tmpRetDetail.tranDate = Format.Date10Format.format(tmpTRANDATE);//tmpTRANDATE.ToString("yyyy-MM-dd");
                                        tmpRetDetail.valueDate = Format.Date10Format.format(tmpTRANDATE);//tmpTRANDATE.ToString("yyyy-MM-dd");
                                        tmpRetDetail.tranTime = Format.DateTimeFormat.format(tmpTRANDATETIME);//tmpTRANDATETIME.ToString("yyyy-MM-dd HH:mm:ss");
                                        tmpRetDetail.billType = tmpRetNode.getSingleTextNotNull("CRE_TYP").trim();
                                        tmpRetDetail.billNum = tmpRetNode.getSingleTextNotNull("CRE_NO").trim();
                                        // #start "MESSAGE 摘要"
                                        String tmpBK_MESSAGE = tmpRetNode.getSingleTextNotNull("MESSAGE").trim();
                                        String tmpBK_DET = tmpRetNode.getSingleTextNotNull("DET").trim();
                                        String messageToNote = getBankConfig().getValueAsString("messageToNote", "comment");
                                        if ("comment".equals(messageToNote)) {
                                            tmpRetDetail.comment = tmpBK_MESSAGE;
                                            tmpRetDetail.usage = tmpBK_DET;
                                        } else {
                                            tmpRetDetail.comment = tmpBK_DET;
                                            tmpRetDetail.usage = tmpBK_MESSAGE;
                                        }
//                                        tmpRetDetail.abstr = pickupAbstr(tmpBK_MESSAGE, tmpBK_DET);
                                        //ATSES-22232 通威目前在用的十个支付渠道,对账码识别截取优化 -----范天星/2020/01/19
                                        if (tmpBK_DET.contains("＋")) {
                                            tmpRetDetail.abstr = tmpBK_DET.substring(tmpBK_DET.lastIndexOf("＋") + 1, tmpBK_DET.length());
                                            tmpRetDetail.usage = tmpBK_DET.substring(0, tmpBK_DET.lastIndexOf("＋"));
                                        } else {
                                            tmpRetDetail.abstr = "";
                                            tmpRetDetail.usage = tmpRetDetail.usage.trim();
                                        }
                                        // #end

                                        // #start "发生额、余额、借贷标志 0-借 1-贷"
                                        String tmpAMT = tmpRetNode.getSingleText("AMT");
                                        String tmpFLAG1 = tmpRetNode.getSingleText("FLAG1");
                                        if (tmpFLAG1.equals("0")) {
                                            if (tmpAMT.indexOf("-") < 0) {//借正，正常业务
                                                tmpRetDetail.tranAmt = tmpAMT;
                                                tmpRetDetail.cdFlag = "1";
                                            } else {//借负，银行退票,表示收入
                                                tmpRetDetail.tranAmt = tmpAMT.replace("-", "");
                                                tmpRetDetail.cdFlag = "2";//借贷表示置为贷，表示收入
                                            }
                                        } else {
                                            if (tmpAMT.indexOf("-") < 0) {//贷正，正常业务
                                                tmpRetDetail.tranAmt = tmpAMT;
                                                tmpRetDetail.cdFlag = "2";
                                            } else {//贷负，目前应该没有这种业务，按照正常的程序逻辑做一下处理
                                                tmpRetDetail.tranAmt = tmpAMT.replace("-", "");
                                                tmpRetDetail.cdFlag = "1";//借贷表示置为借，表示支出
                                            }
                                        }
                                        tmpRetDetail.accBal = tmpRetNode.getSingleText("AMT1");
                                        // #end

                                        tmpRetDetail.recAccNum = tmpRetNode.getSingleTextNotNull("ACCNO2").trim();
                                        tmpRetDetail.recAccName = tmpRetNode.getSingleTextNotNull("ACC_NAME1").trim();
                                        tmpRetDetail.recAccBank = tmpRetNode.getSingleTextNotNull("CADBank_Nm").trim();
                                        //<TRAN_FLOW>交易流水号</TRAN_FLOW>
                                        tmpRetDetail.payCode = tmpRetNode.getSingleTextNotNull("TRAN_FLOW").trim();
                                        //<BFLOW>企业支付流水号</BFLOW>
                                        String receiptNo = tmpRetNode.getSingleTextNotNull("TRAN_FLOW").trim();
                                        WriteBankLogLn("处理前回单编号:" + receiptNo);
                                        receiptNo = antiTransferStr(receiptNo);
                                        WriteBankLogLn("处理后回单编号:" + receiptNo);
                                        tmpRetDetail.receiptNo = receiptNo;
                                        tmpRetDetail.askNumber = tmpRetNode.getSingleTextNotNull("BFLOW").trim();
                                        tmpRetDetail.reqBatchNO = tmpRetNode.getSingleTextNotNull("BFLOW").trim();
                                        tmpRetDetail.originator = tmpRetNode.getSingleText("TRAN_FLOW");

                                        nodeAnalysic(tmpBkReader.readedXElement, tmpRetDetail, getCharset());

                                        retAts.writeRetDetail(tmpRetDetail);
                                    }
                                    // #end
                                }
                            } else {
                                // #start "查询失败"
                                tmpRespCode = "-1";
                                tmpRespInfo = tmpBkReader.getFirstText("/TX/RETURN_MSG");
                                break;
                                // #end
                            }
                        } else {
                            tmpRespCode = "-1";
                            tmpRespInfo = tmpSbMsg.toString();
                            break;
                        }
                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = tmpStrRet[1];
                        break;
                    }
                    if (tmpNowPage >= tmpTotalPage) {
                        break;
                    }
                    tmpNowPage++;
                }

                tmpCalendarFrom = tmpClndrStepTo;
                tmpCalendarFrom.add(Calendar.DAY_OF_MONTH, 1);
                tmpIntDateFrom = Integer.parseInt(Format.Date8Format.format(tmpCalendarFrom.getTime()));
                tmpIntDateTo = Integer.parseInt(Format.Date8Format.format(tmpCalendarTo.getTime()));
            }
        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        if (tmpRespCode.equals("-1")) {
            retAts = new RetAtsTrans(p_req7516.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("交易为空");
        }
        retAts.writeEnd();
        return retAts;
    }

    //当日明细查询调用历史明细查询方法
    public RetAtsTrans d_7515_TodayTrans(Req7515TodayTrans p_req7515) throws Exception {
        //通用b2e0035
        Req7516HisTrans tmpReq7516 = new Req7516HisTrans(p_req7515.reqHeader);
        tmpReq7516.actArea = p_req7515.actArea;
        tmpReq7516.actCur = p_req7515.actCur;
        tmpReq7516.actNum = p_req7515.actNum;
        String tmpStrToday = Format.Date8Format.format(new Date());
        tmpReq7516.dateFrom = tmpStrToday;
        tmpReq7516.dateTo = tmpStrToday;
        tmpReq7516.actBankType = p_req7515.actBankType;
        return d_7516_HisTrans(tmpReq7516);
    }
}
